package com.zmj.sy.mom.srv.aps.utils;

import com.zmj.sy.mom.srv.aps.bean.bo.ApsMachine;
import com.zmj.sy.mom.srv.aps.bean.bo.ApsTask;
import com.zmj.sy.mom.srv.aps.bean.entity.base.PhaseJob;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ImportFramePhase;
import com.zmj.sy.mom.srv.aps.bean.entity.order.OrderPhase;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Phase;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class TaskTimeUtils {

    private static final String phaseFun1 = "EXEC Custom.NxLmCalcFlowTimeForOlphase <nxolphaseid>, <timingtype>, <WorkLoadHours>, <WorkLoadLeftHours>, <OrderWorkPlaceLoadLeftHours>";
    private static final String phaseFun2 = "SELECT Custom.CalcFlowTimeZMJ_FJ (<nxolphaseid> , <WorkLoadHours>, <WorkLoadLeftHours>)";


    public static Integer calcWlTime(Phase phase) {
        if (StringUtils.hasText(phase.getFlowTimeValue())) {
            if(phase.getFlowTimeType().equals(1)){
                if (phase.getFlowTimeUnit().equals(1)) {
                    return new BigDecimal(phase.getFlowTimeValue()).multiply(new BigDecimal( 60 * 60 * 24)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getFlowTimeUnit().equals(2)) {
                    return new BigDecimal(phase.getFlowTimeValue()).multiply(new BigDecimal( 60 * 60)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getFlowTimeUnit().equals(3)) {
                    return new BigDecimal(phase.getFlowTimeValue()).multiply(new BigDecimal( 60)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getFlowTimeUnit().equals(4)) {
                    return new BigDecimal(phase.getFlowTimeValue()).setScale(0, RoundingMode.UP).intValue();
                } else {
                    throw SyExceptionUtils.e("工序【{}】的单位未知", phase.getMyCode(), phase.getFlowTimeUnit());
                }
            } else {
                throw SyExceptionUtils.e("WL件的工序时长为固定工时", phase.getMyCode());
            }

        }
        throw SyExceptionUtils.e("WL件的工序时长为固定工时", phase.getMyCode());

    }


    public static Integer calcTime(ApsTask currentTask, Phase phase, ApsMachine machine, List<ApsMachine> machines, Map<String, Map<String, Integer>> phaseGroupWorkTimeMap) {

        if (StringUtils.hasText(phase.getFlowTimeValue())) {

            int cacheTime = 0;
            if (StringUtils.hasText(phase.getCacheTimeValue())) {
                if (phase.getCacheTimeUnit() == null){
                    throw SyExceptionUtils.e("工序缓存单位【{}】的单位未知", phase.getMyCode(), phase.getFlowTimeUnit());
                } else if (phase.getCacheTimeUnit().equals(1)) {
                    cacheTime = new BigDecimal(phase.getCacheTimeValue()).multiply(new BigDecimal(60 * 60 * 24)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getCacheTimeUnit().equals(2)) {
                    cacheTime = new BigDecimal(phase.getCacheTimeValue()).multiply(new BigDecimal(60 * 60)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getCacheTimeUnit().equals(3)) {
                    cacheTime = new BigDecimal(phase.getCacheTimeValue()).multiply(new BigDecimal(60)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getCacheTimeUnit().equals(4)) {
                    cacheTime = new BigDecimal(phase.getCacheTimeValue()).setScale(0, RoundingMode.UP).intValue();
                } else {
                    throw SyExceptionUtils.e("工序缓存单位【{}】的单位未知", phase.getMyCode(), phase.getFlowTimeUnit());
                }
            }

            if (phase.getFlowTimeType().equals(1)) {
                if (phase.getFlowTimeUnit().equals(1)) {
                    return new BigDecimal(phase.getFlowTimeValue()).multiply(new BigDecimal(60 * 60 * 24)).add(new BigDecimal(cacheTime)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getFlowTimeUnit().equals(2)) {
                    return new BigDecimal(phase.getFlowTimeValue()).multiply(new BigDecimal(60 * 60)).add(new BigDecimal(cacheTime)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getFlowTimeUnit().equals(3)) {
                    return new BigDecimal(phase.getFlowTimeValue()).multiply(new BigDecimal(60)).add(new BigDecimal(cacheTime)).setScale(0, RoundingMode.UP).intValue();
                } else if (phase.getFlowTimeUnit().equals(4)) {
                    return new BigDecimal(phase.getFlowTimeValue()).add(new BigDecimal(cacheTime)).setScale(0, RoundingMode.UP).intValue();
                } else {
                    throw SyExceptionUtils.e("工序【{}】的单位未知", phase.getMyCode(), phase.getFlowTimeUnit());
                }
            } else if (phase.getFlowTimeType().equals(2)) {
                if (phase.getFlowTimeValue().equals(phaseFun1)) {
                    return phaseFun1(currentTask, phase, machine, machines, phaseGroupWorkTimeMap);
                }
            }
        }

        throw SyExceptionUtils.e("未知的工序{}时间类型", phase.getMyCode());
    }

    private static Integer phaseFun1(ApsTask currentTask, Phase phase, ApsMachine machine, List<ApsMachine> machines, Map<String, Map<String, Integer>> phaseGroupWorkTimeMap){

        // 判断是否有缓存时间
        int cacheTime = 0;
        if (StringUtils.hasText(phase.getCacheTimeValue())) {
            if (phase.getCacheTimeUnit().equals(1)) {
                cacheTime = new BigDecimal(phase.getCacheTimeValue()).multiply(new BigDecimal(60 * 60 * 24)).setScale(0, RoundingMode.UP).intValue();
            } else if (phase.getCacheTimeUnit().equals(2)) {
                cacheTime = new BigDecimal(phase.getCacheTimeValue()).multiply(new BigDecimal( 60 * 60)).setScale(0, RoundingMode.UP).intValue();
            } else if (phase.getCacheTimeUnit().equals(3)) {
                cacheTime = new BigDecimal(phase.getCacheTimeValue()).multiply(new BigDecimal( 60)).setScale(0, RoundingMode.UP).intValue();
            } else if (phase.getCacheTimeUnit().equals(4)) {
                cacheTime = new BigDecimal(phase.getCacheTimeValue()).setScale(0, RoundingMode.UP).intValue();
            } else {
                throw SyExceptionUtils.e("工序缓存单位【{}】的单位未知", phase.getMyCode(), phase.getFlowTimeUnit());
            }
        }


        if (currentTask.getApsWorkOrder().getMaterType().equals("Assembly")) {

            // 如果是是组件，如果是PH, LH, BH,则取数据库的流动时间，否则为默认的一小时
            // 工时为：单价工时 / 工作能力 / 60 * 1.2 * 60

            return new BigDecimal(currentTask.getApsWorkDetail().getWorkTime()).divide(new BigDecimal(60), 7, RoundingMode.UP) // 单件工时分
                    .divide(machine.getStation().getWorkAbility(), 7, RoundingMode.UP)// 工作能力
                    .divide(new BigDecimal(60), 0, RoundingMode.UP) // 工时分
                    .multiply(machine.getStation().getProductivity()) // 生产效率 1.2
                    .multiply(new BigDecimal(60)) // 加倍
                    .multiply(new BigDecimal(60)) // 秒
                    .add(new BigDecimal(cacheTime))
                    .setScale(0, RoundingMode.UP)
                    .intValue();
        }

        if (currentTask.getApsWorkOrder().getMaterType().equals("Platepart")) {
            List<String> phaseList = Arrays.asList("JG", "JP", "PK", "GC", "BX", "CX", "BT", "BZ", "XC", "HG", "ZW");
            String phaseCode = currentTask.getApsWorkDetail().getPhaseCode().replaceAll("\\d+$", "");
            if (phaseList.contains(phaseCode)) {
                return new BigDecimal(phaseGroupWorkTimeMap.get(currentTask.getApsWorkDetail().getPhaseCode()).get(currentTask.getApsWorkDetail().getGroupCode())).divide(new BigDecimal(60), 7, RoundingMode.UP) // 单件工时分
//                            .multiply(currentTask.getApsWorkDetail().getTotalCount())  // 数量
                        .divide(new BigDecimal(machines.size()), 7, RoundingMode.UP)
                        .divide(new BigDecimal(60), 0, RoundingMode.UP)
                        .multiply(machine.getStation().getProductivity()) // 生产效率 1.2
                        .multiply(new BigDecimal(60))
                        .multiply(new BigDecimal(60)) // 秒
                        .add(new BigDecimal(cacheTime))
                        .setScale(0, RoundingMode.UP)
                        .intValue();

            } else {
                return new BigDecimal("0.5")
                        .multiply(machine.getStation().getProductivity())
                        .multiply(new BigDecimal(60))
                        .multiply(new BigDecimal(60))//秒
                        .add(new BigDecimal(cacheTime))
                        .setScale(0, RoundingMode.UP)
                        .intValue();
            }
        }
//        if (phaseExt == null || phaseExt.getFlowTime() == null) {
//
//        } else {
//            allWorkTime = phaseExt.getFlowTime().multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).setScale(0, RoundingMode.UP).intValue();
//        }

        throw SyExceptionUtils.e("没有找到匹配的工序");
//        return 0;
    }

    public static int calcJobTime(OrderPhase phase, List<PhaseJob> job) {
        int jobTime = 0;
        for (PhaseJob phaseJob : job) {
            if(phaseJob.getName().equals("WorkTimeFromXML")){
                continue;
            }
            if (1 == phaseJob.getUseTime()) {
                if(1 == phaseJob.getTimeType()){
                    jobTime += new BigDecimal(phaseJob.getTimeValue()).multiply(new BigDecimal(60)).setScale(0, RoundingMode.UP).intValue();
                }
            }
        }
        return jobTime;
    }

    public static int calcJobTime(ImportFramePhase phase, List<PhaseJob> job) {
        int jobTime = 0;
        for (PhaseJob phaseJob : job) {
            if(phaseJob.getName().equals("WorkTimeFromXML")){
                continue;
            }
            if (1 == phaseJob.getUseTime()) {
                if(1 == phaseJob.getTimeType()){
                    jobTime += new BigDecimal(phaseJob.getTimeValue()).multiply(new BigDecimal(60)).setScale(0, RoundingMode.UP).intValue();
                }
            }
        }
        return jobTime;
    }
}
